package org.example.springbootdemo.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
public class AsyncServiceImpl implements AsyncService{

    @Override
//    @Async
    @Async(value = "threadPool_1")
    public void simpleAsync() {
        System.out.println("ThreadName【{}】异步方法开始……"+Thread.currentThread().getName());
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("ThreadName【{}】异步方法结束。"+ Thread.currentThread().getName());
    }
    //继承Thread类
    @Override
    public void simpleThread1() {
        Thread thread = new Thread(() -> {
            String threadName = Thread.currentThread().getName();
            System.out.println(String.format("ThreadName【%s】线程开始执行……", threadName));
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(String.format("ThreadName【%s】线程执行结束。", threadName));
        });
        thread.start();
    }

    @Override
    public void simpleThread2() {
        Runnable runnable = () -> {
            String threadName = Thread.currentThread().getName();
            System.out.println(String.format("ThreadName【%s】线程开始执行……", threadName));
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(String.format("ThreadName【%s】线程执行结束。", threadName));
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

}
